/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.crystalrange.documents.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.documents.model.DocumentReferenceType;
import org.crystalrange.documents.service.DocumentReferenceTypeService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

/**
 *
 * @author Krzysiek
 */
public class DocumentReferenceTypeListController extends BaseController {
    protected DocumentReferenceTypeService documentReferenceTypeService;
    protected DocumentReferenceType selectedDocumentReferenceType;
    protected List<DocumentReferenceType> documentReferenceTypes;
    protected Listbox listboxDocumentReferenceType;
    protected Search searchDocumentReferenceType;

    public DocumentReferenceTypeService getDocumentReferenceTypeService() {
        return documentReferenceTypeService;
    }

    public void setDocumentReferenceTypeService(DocumentReferenceTypeService documentReferenceTypeService) {
        this.documentReferenceTypeService = documentReferenceTypeService;
    }

    public List<DocumentReferenceType> getDocumentReferenceTypes() {
        return documentReferenceTypes;
    }

    public void setDocumentReferenceTypes(List<DocumentReferenceType> documentReferenceTypes) {
        this.documentReferenceTypes = documentReferenceTypes;
    }

    public DocumentReferenceType getSelectedDocumentReferenceType() {
        return selectedDocumentReferenceType;
    }

    public void setSelectedDocumentReferenceType(DocumentReferenceType selectedDocumentReferenceType) {
        this.selectedDocumentReferenceType = selectedDocumentReferenceType;
    }

    public void onClick$toolbarbuttonDocumentReferenceTypeCreate(Event event) throws Exception {
        this.documentReferenceTypeCreate();
    }
    
    public void onClick$menuitemDocumentReferenceTypeCreate(Event event) throws Exception {
        this.documentReferenceTypeCreate();
    }

    public void onClick$toolbarbuttonDocumentReferenceTypeEdit(Event event) throws Exception {
        this.documentReferenceTypeEdit();
    }
    
    public void onClick$menuitemDocumentReferenceTypeEdit(Event event) throws Exception {
        this.documentReferenceTypeEdit();
    }
    
    public void onClick$toolbarbuttonDocumentReferenceTypeDelete(Event event) throws Exception {
        this.documentReferenceTypeDelete();
    }
    
    public void onClick$menuitemDocumentReferenceTypeDelete(Event event) throws Exception {
        this.documentReferenceTypeDelete();
    }
    
    public void onClick$toolbarbuttonDocumentReferenceTypeSearch(Event event) throws Exception {
        this.documentReferenceTypeSearch();
    }
    
    public void onClick$menuitemDocumentReferenceTypeSearch(Event event) throws Exception {
        this.documentReferenceTypeSearch();
    }
    
    public void onClick$toolbarbuttonDocumentReferenceTypeRefresh(Event event) throws Exception {
        this.documentReferenceTypeRefresh();
    }
    
    public void onClick$menuitemDocumentReferenceTypeRefresh(Event event) throws Exception {
        this.documentReferenceTypeRefresh();
    }
    
    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);
        this.searchDocumentReferenceType = new Search(DocumentReferenceType.class);
        this.searchDocumentReferenceType.setMaxResults(100);
        this.searchDocumentReferenceType.addFetch("createdBy");
        this.documentReferenceTypes = this.documentReferenceTypeService.search(this.searchDocumentReferenceType);
        component.setAttribute("controller", this);
    }
    
    
    private void documentReferenceTypeCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/documentReferenceType.zul");
    }
    
    private void documentReferenceTypeEdit() throws Exception {
        if (this.selectedDocumentReferenceType != null) {
            this.execution.sendRedirect("~./zul/html/documentReferenceType.zul?Id=" + this.selectedDocumentReferenceType.getId().toString());
        }
    }
    
    private void documentReferenceTypeDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxDocumentReferenceType.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    DocumentReferenceType documentReferenceType = (DocumentReferenceType) selectedItem.getValue();
                    this.documentReferenceTypeService.remove(documentReferenceType);
                }
                this.selectedDocumentReferenceType = null;
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.documentReferenceTypes = this.documentReferenceTypeService.search(this.searchDocumentReferenceType);
        this.binder.loadAll();
    }
    
    private void documentReferenceTypeSearch() throws Exception {
        this.execution.setAttribute("searchDocumentReferenceType", this.searchDocumentReferenceType);

        Window window = (Window) this.execution.createComponents("~./zul/html/documentReferenceTypeSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchDocumentReferenceType = (Search) this.execution.getAttribute("searchDocumentReferenceType");
        }

        this.documentReferenceTypes = this.documentReferenceTypeService.search(this.searchDocumentReferenceType);
        this.binder.loadAll();
    }
    
    private void documentReferenceTypeRefresh() throws Exception {
        this.documentReferenceTypes = this.documentReferenceTypeService.search(this.searchDocumentReferenceType);
        this.binder.loadAll();
    }
}
